/*
    需求：
         开关
         启动就能用

        函数哪里改成对应的 s 键，其它也是。

        idea alt + s 就能复制。
        加数字测试了
        在通用哪个插件，调用这个插件里面的快捷键了？？？






        ; 以下都是小键盘的数字
       ctrl + 5 = 鼠标中键点击。
        ctrl + 7和1 = 鼠标加速移动，7加多少，1减回来。刚按下移动速度。20
        ctrl + 8和2 = 鼠标速度。按一下移动多少像数  2
        ctrl + 9和3 = 鼠标最大速度，一直按下移动的速度。50

        alt + 7和1 = 向下滚动的速度
        alt + 8和2 = 向下滚动多少行
        alt + 9和3 = 鼠标滚轮最大速度，一直按下时 加的移动的速度

        4和6 = 鼠标移动变化，左上角变右上角(旋转角度)，如：把7(u)变成9(o)

*/

NumpadMouse:

    ; 8和2 调速度
    MouseSpeed = 1
    ; 7和1
    MouseAccelerationSpeed = 60
    ; 9和3
    MouseMaxSpeed = 24

    ; 鼠标滚轮
    MouseWheelSpeed = 1
    MouseWheelAccelerationSpeed = 1
    MouseWheelMaxSpeed = 5

    ; 4和6 鼠标旋转角
    MouseRotationAngle = 0


    Temp = 0
    Temp2 = 0

    MouseRotationAnglePart = %MouseRotationAngle%
    MouseRotationAnglePart /= 45

    MouseCurrentAccelerationSpeed = 0
    MouseCurrentSpeed = %MouseSpeed%

    MouseWheelCurrentAccelerationSpeed = 0
    MouseWheelCurrentSpeed = %MouseSpeed%

    SetKeyDelay, -1
    SetMouseDelay, -1

    ; 显示提示信息
    showToolTipStatus := true


;    global winObj22 := vim.GetWin(NumpadMouse)


    vim.comment("<ButtonLeftClick>", "鼠标左键")
    vim.comment("<ButtonLeftClickIns>", "鼠标左键02")
    vim.comment("<ButtonMiddleClick>", "鼠标滚轮")
    vim.comment("<ButtonMiddleClickClear>", "鼠标滚轮02")
    vim.comment("<ButtonRightClick>", "鼠标右键")
;    vim.comment("<ButtonRightClickDel>", "功能2")
    vim.comment("<ButtonX1Click>", "前进")
    vim.comment("<ButtonX2Click>", "后退")
    vim.comment("<ButtonWheelUp>", "鼠标滚轮-向下滚动")
    vim.comment("<ButtonWheelDown>", "鼠标滚轮-向上滚动")
    vim.comment("<ButtonUp>", "光标向上")
    vim.comment("<ButtonDown>", "光标向下")
    vim.comment("<ButtonLeft>", "光标向左")
    vim.comment("<ButtonRight>", "光标向右")
    vim.comment("<ButtonUpLeft>", "光标左上角")
    vim.comment("<ButtonUpRight>", "光标右上角")
    vim.comment("<ButtonDownLeft>", "光标左下角")
    vim.comment("<ButtonDownRight>", "光标右下角")
    vim.comment("<ButtonSpeedUp>", "按一下移动多少像数 加")
    vim.comment("<ButtonSpeedDown>", "按一下移动多少像数 减")
    vim.comment("<ButtonAccelerationSpeedUp>", "刚按下移动速度 加")
    vim.comment("<ButtonAccelerationSpeedDown>", "刚按下移动速度 减")
    vim.comment("<ButtonMaxSpeedUp>", "一直按下移动的速度 加")
    vim.comment("<ButtonMaxSpeedDown>", "一直按下移动的速度 减")
    vim.comment("<ButtonRotationAngleUp>", "旋转角度 u变o")
    vim.comment("<ButtonRotationAngleDown>", "旋转角度 u变o")
    vim.comment("<ButtonWheelSpeedUp>", "向下滚动多少行  加")
    vim.comment("<ButtonWheelSpeedDown>", "向下滚动多少行 减")
    vim.comment("<ButtonWheelAccelerationSpeedUp>", "向下滚动的速度 加")
    vim.comment("<ButtonWheelAccelerationSpeedDown>", "向下滚动的速度 减")
    vim.comment("<ButtonWheelMaxSpeedUp>", "鼠标滚轮最大速度一直按下时加的移动的速度 加")
    vim.comment("<ButtonWheelMaxSpeedDown>", "鼠标滚轮最大速度一直按下时加的移动的速度 减")


    vim.comment("<toggleNumpadMouse>", "启用/禁用鼠标键盘")




    vim.SetWin("NumpadMouse", "Chrome_WidgetWin_1")
    vim.SetMode("normal", "NumpadMouse")




;    vim.Map("s", "<ButtonLeftClick>", "NumpadMouse")
;    vim.Map("s", "<ButtonLeftClickIns>", "NumpadMouse")

    vim.Map("<c-NumPad5>", "<ButtonMiddleClick>", "NumpadMouse")
    vim.Map("<c-NumpadClear>", "<ButtonMiddleClickClear>", "NumpadMouse")

;    vim.Map("a", "<ButtonRightClick>", "NumpadMouse")
;;    vim.Map("NumPadDel", "<ButtonRightClickDel>", "NumpadMouse")

;    vim.Map("[", "<ButtonX1Click>", "NumpadMouse")
;    vim.Map("]", "<ButtonX2Click>", "NumpadMouse")


;    vim.Map("f", "<ButtonWheelUp>", "NumpadMouse")
;    vim.Map("e", "<ButtonWheelDown>", "NumpadMouse")

;    vim.Map("k", "<ButtonUp>", "NumpadMouse")
;    vim.Map("j", "<ButtonDown>", "NumpadMouse")
;    vim.Map("h", "<ButtonLeft>", "NumpadMouse")
;    vim.Map("l", "<ButtonRight>", "NumpadMouse")
;    vim.Map("u", "<ButtonUpLeft>", "NumpadMouse")
;    vim.Map("o", "<ButtonUpRight>", "NumpadMouse")
;    vim.Map("n", "<ButtonDownLeft>", "NumpadMouse")
;    vim.Map("m", "<ButtonDownRight>", "NumpadMouse")



    vim.Map("<c-Numpad8>", "<ButtonSpeedUp>", "NumpadMouse")
    vim.Map("<c-Numpad2>", "<ButtonSpeedDown>", "NumpadMouse")
    vim.Map("<c-Numpad7>", "<ButtonAccelerationSpeedUp>", "NumpadMouse")
    vim.Map("<c-Numpad1>", "<ButtonAccelerationSpeedDown>", "NumpadMouse")
    vim.Map("<c-Numpad9>", "<ButtonMaxSpeedUp>", "NumpadMouse")
    vim.Map("<c-Numpad3>", "<ButtonMaxSpeedDown>", "NumpadMouse")

    vim.Map("<c-Numpad6>", "<ButtonRotationAngleUp>", "NumpadMouse")
    vim.Map("<c-Numpad4>", "<ButtonRotationAngleDown>", "NumpadMouse")

    vim.Map("<a-Numpad8>", "<ButtonWheelSpeedUp>", "NumpadMouse")
    vim.Map("<a-Numpad2>", "<ButtonWheelSpeedDown>", "NumpadMouse")
    vim.Map("<a-Numpad7>", "<ButtonWheelAccelerationSpeedUp>", "NumpadMouse")
    vim.Map("<a-Numpad1>", "<ButtonWheelAccelerationSpeedDown>", "NumpadMouse")
    vim.Map("<a-Numpad9>", "<ButtonWheelMaxSpeedUp>", "NumpadMouse")
    vim.Map("<a-Numpad3>", "<ButtonWheelMaxSpeedDown>", "NumpadMouse")

    ; 启用/禁用鼠标键盘
;    vim.Map("c", "<toggleNumpadMouse>", "NumpadMouse")

return

; 启用/禁用鼠标键盘
<toggleNumpadMouse>:
    vim.Toggle("NumpadMouse")
return



;Mouse click support

<ButtonLeftClick>:
GetKeyState, already_down_state, LButton
If already_down_state = D
    return
Button2 = s
ButtonClick = Left
Goto ButtonClickStart
<ButtonLeftClickIns>:
GetKeyState, already_down_state, LButton
If already_down_state = D
    return
Button2 = s
ButtonClick = Left
Goto ButtonClickStart

<ButtonMiddleClick>:
GetKeyState, already_down_state, MButton
If already_down_state = D
    return
Button2 = NumPad5
ButtonClick = Middle
Goto ButtonClickStart
<ButtonMiddleClickClear>:
GetKeyState, already_down_state, MButton
If already_down_state = D
    return
Button2 = NumPadClear
ButtonClick = Middle
Goto ButtonClickStart

<ButtonRightClick>:
GetKeyState, already_down_state, RButton
If already_down_state = D
    return
Button2 = NumPadDot
ButtonClick = Right
Goto ButtonClickStart

;<ButtonRightClickDel>:
;GetKeyState, already_down_state, RButton
;If already_down_state = D
;    return
;Button2 = NumPadDel
;ButtonClick = Right
;Goto ButtonClickStart

<ButtonX1Click>:
GetKeyState, already_down_state, XButton1
If already_down_state = D
    return
Button2 = NumPadDiv
ButtonClick = X1
Goto ButtonClickStart

<ButtonX2Click>:
GetKeyState, already_down_state, XButton2
If already_down_state = D
    return
Button2 = NumPadMult
ButtonClick = X2
Goto ButtonClickStart

ButtonClickStart:
MouseClick, %ButtonClick%,,, 1, 0, D
SetTimer, ButtonClickEnd, 10
return

ButtonClickEnd:
GetKeyState, kclickstate, %Button2%, P
if kclickstate = D
    return

SetTimer, ButtonClickEnd, off
MouseClick, %ButtonClick%,,, 1, 0, U
return

;Mouse movement support

<ButtonSpeedUp>:
MouseSpeed++
ToolTip, Mouse speed: %MouseSpeed% pixels
SetTimer, RemoveToolTip, 1000
return


<ButtonSpeedDown>:
If MouseSpeed > 1
    MouseSpeed--
If MouseSpeed = 1
    ToolTip, Mouse speed: %MouseSpeed% pixel
else
    ToolTip, Mouse speed: %MouseSpeed% pixels
SetTimer, RemoveToolTip, 1000
return
<ButtonAccelerationSpeedUp>:
MouseAccelerationSpeed++
ToolTip, Mouse acceleration speed: %MouseAccelerationSpeed% pixels
SetTimer, RemoveToolTip, 1000
return
<ButtonAccelerationSpeedDown>:
If MouseAccelerationSpeed > 1
    MouseAccelerationSpeed--
If MouseAccelerationSpeed = 1
    ToolTip, Mouse acceleration speed: %MouseAccelerationSpeed% pixel
else
    ToolTip, Mouse acceleration speed: %MouseAccelerationSpeed% pixels
SetTimer, RemoveToolTip, 1000
return

<ButtonMaxSpeedUp>:
MouseMaxSpeed++
ToolTip, Mouse maximum speed: %MouseMaxSpeed% pixels
SetTimer, RemoveToolTip, 1000
return
<ButtonMaxSpeedDown>:
If MouseMaxSpeed > 1
    MouseMaxSpeed--
If MouseMaxSpeed = 1
    ToolTip, Mouse maximum speed: %MouseMaxSpeed% pixel
else
    ToolTip, Mouse maximum speed: %MouseMaxSpeed% pixels
SetTimer, RemoveToolTip, 1000
return

<ButtonRotationAngleUp>:
MouseRotationAnglePart++
If MouseRotationAnglePart >= 8
    MouseRotationAnglePart = 0
MouseRotationAngle = %MouseRotationAnglePart%
MouseRotationAngle *= 45
ToolTip, Mouse rotation angle: %MouseRotationAngle%°
SetTimer, RemoveToolTip, 1000
return
<ButtonRotationAngleDown>:
MouseRotationAnglePart--
If MouseRotationAnglePart < 0
    MouseRotationAnglePart = 7
MouseRotationAngle = %MouseRotationAnglePart%
MouseRotationAngle *= 45
ToolTip, Mouse rotation angle: %MouseRotationAngle%°
SetTimer, RemoveToolTip, 1000
return

<ButtonUp>:
<ButtonDown>:
<ButtonLeft>:
<ButtonRight>:
<ButtonUpLeft>:
<ButtonUpRight>:
<ButtonDownLeft>:
<ButtonDownRight>:
If Button <> 0
{
    IfNotInString, A_ThisHotkey, %Button%
    {
        MouseCurrentAccelerationSpeed = 0
        MouseCurrentSpeed = %MouseSpeed%
    }
}
StringReplace, Button, A_ThisHotkey, *

ButtonAccelerationStart:
If MouseAccelerationSpeed >= 1
{
    If MouseMaxSpeed > %MouseCurrentSpeed%
    {
        Temp = 0.001
        Temp *= %MouseAccelerationSpeed%
        MouseCurrentAccelerationSpeed += %Temp%
        MouseCurrentSpeed += %MouseCurrentAccelerationSpeed%
    }
}

;MouseRotationAngle convertion to speed of button direction
{
    MouseCurrentSpeedToDirection = %MouseRotationAngle%
    MouseCurrentSpeedToDirection /= 90.0
    Temp = %MouseCurrentSpeedToDirection%

    if Temp >= 0
    {
        if Temp < 1
        {
            MouseCurrentSpeedToDirection = 1
            MouseCurrentSpeedToDirection -= %Temp%
            Goto EndMouseCurrentSpeedToDirectionCalculation
        }
    }
    if Temp >= 1
    {
        if Temp < 2
        {
            MouseCurrentSpeedToDirection = 0
            Temp -= 1
            MouseCurrentSpeedToDirection -= %Temp%
            Goto EndMouseCurrentSpeedToDirectionCalculation
        }
    }
    if Temp >= 2
    {
        if Temp < 3
        {
            MouseCurrentSpeedToDirection = -1
            Temp -= 2
            MouseCurrentSpeedToDirection += %Temp%
            Goto EndMouseCurrentSpeedToDirectionCalculation
        }
    }
    if Temp >= 3
    {
        if Temp < 4
        {
            MouseCurrentSpeedToDirection = 0
            Temp -= 3
            MouseCurrentSpeedToDirection += %Temp%
            Goto EndMouseCurrentSpeedToDirectionCalculation
        }
    }
}
EndMouseCurrentSpeedToDirectionCalculation:

;MouseRotationAngle convertion to speed of 90 degrees to right
{
    MouseCurrentSpeedToSide = %MouseRotationAngle%
    MouseCurrentSpeedToSide /= 90.0
    Temp = %MouseCurrentSpeedToSide%
    Transform, Temp, mod, %Temp%, 4

    if Temp >= 0
    {
        if Temp < 1
        {
            MouseCurrentSpeedToSide = 0
            MouseCurrentSpeedToSide += %Temp%
            Goto EndMouseCurrentSpeedToSideCalculation
        }
    }
    if Temp >= 1
    {
        if Temp < 2
        {
            MouseCurrentSpeedToSide = 1
            Temp -= 1
            MouseCurrentSpeedToSide -= %Temp%
            Goto EndMouseCurrentSpeedToSideCalculation
        }
    }
    if Temp >= 2
    {
        if Temp < 3
        {
            MouseCurrentSpeedToSide = 0
            Temp -= 2
            MouseCurrentSpeedToSide -= %Temp%
            Goto EndMouseCurrentSpeedToSideCalculation
        }
    }
    if Temp >= 3
    {
        if Temp < 4
        {
            MouseCurrentSpeedToSide = -1
            Temp -= 3
            MouseCurrentSpeedToSide += %Temp%
            Goto EndMouseCurrentSpeedToSideCalculation
        }
    }
}
EndMouseCurrentSpeedToSideCalculation:

MouseCurrentSpeedToDirection *= %MouseCurrentSpeed%
MouseCurrentSpeedToSide *= %MouseCurrentSpeed%

Temp = %MouseRotationAnglePart%
Transform, Temp, Mod, %Temp%, 2

If Button = k
{
    if Temp = 1
    {
        MouseCurrentSpeedToSide *= 2
        MouseCurrentSpeedToDirection *= 2
    }

    MouseCurrentSpeedToDirection *= -1
    MouseMove, %MouseCurrentSpeedToSide%, %MouseCurrentSpeedToDirection%, 0, R
}
else if Button = j
{
    if Temp = 1
    {
        MouseCurrentSpeedToSide *= 2
        MouseCurrentSpeedToDirection *= 2
    }

    MouseCurrentSpeedToSide *= -1
    MouseMove, %MouseCurrentSpeedToSide%, %MouseCurrentSpeedToDirection%, 0, R
}
else if Button = h
{
    if Temp = 1
    {
        MouseCurrentSpeedToSide *= 2
        MouseCurrentSpeedToDirection *= 2
    }

    MouseCurrentSpeedToSide *= -1
    MouseCurrentSpeedToDirection *= -1

    MouseMove, %MouseCurrentSpeedToDirection%, %MouseCurrentSpeedToSide%, 0, R
}
else if Button = l
{
    if Temp = 1
    {
        MouseCurrentSpeedToSide *= 2
        MouseCurrentSpeedToDirection *= 2
    }

    MouseMove, %MouseCurrentSpeedToDirection%, %MouseCurrentSpeedToSide%, 0, R
}
else if Button = u
{
    Temp = %MouseCurrentSpeedToDirection%
    Temp -= %MouseCurrentSpeedToSide%
    Temp *= -1
    Temp2 = %MouseCurrentSpeedToDirection%
    Temp2 += %MouseCurrentSpeedToSide%
    Temp2 *= -1
    MouseMove, %Temp%, %Temp2%, 0, R
}
else if Button = o
{
    Temp = %MouseCurrentSpeedToDirection%
    Temp += %MouseCurrentSpeedToSide%
    Temp2 = %MouseCurrentSpeedToDirection%
    Temp2 -= %MouseCurrentSpeedToSide%
    Temp2 *= -1
    MouseMove, %Temp%, %Temp2%, 0, R
}
else if Button = n
{
    Temp = %MouseCurrentSpeedToDirection%
    Temp += %MouseCurrentSpeedToSide%
    Temp *= -1
    Temp2 = %MouseCurrentSpeedToDirection%
    Temp2 -= %MouseCurrentSpeedToSide%
    MouseMove, %Temp%, %Temp2%, 0, R
}
else if Button = m
{
    Temp = %MouseCurrentSpeedToDirection%
    Temp -= %MouseCurrentSpeedToSide%
    Temp2 *= -1
    Temp2 = %MouseCurrentSpeedToDirection%
    Temp2 += %MouseCurrentSpeedToSide%
    MouseMove, %Temp%, %Temp2%, 0, R
}

SetTimer, ButtonAccelerationEnd, 10
return

ButtonAccelerationEnd:
GetKeyState, kstate, %Button%, P
if kstate = D
    Goto ButtonAccelerationStart

SetTimer, ButtonAccelerationEnd, off
MouseCurrentAccelerationSpeed = 0
MouseCurrentSpeed = %MouseSpeed%
Button = 0
return

;Mouse wheel movement support

<ButtonWheelSpeedUp>:
MouseWheelSpeed++
RegRead, MouseWheelSpeedMultiplier, HKCU, Control Panel\Desktop, WheelScrollLines
If MouseWheelSpeedMultiplier <= 0
    MouseWheelSpeedMultiplier = 1
MouseWheelSpeedReal = %MouseWheelSpeed%
MouseWheelSpeedReal *= %MouseWheelSpeedMultiplier%
ToolTip, Mouse wheel speed: %MouseWheelSpeedReal% lines
SetTimer, RemoveToolTip, 1000
return
<ButtonWheelSpeedDown>:
RegRead, MouseWheelSpeedMultiplier, HKCU, Control Panel\Desktop, WheelScrollLines
If MouseWheelSpeedMultiplier <= 0
    MouseWheelSpeedMultiplier = 1
If MouseWheelSpeedReal > %MouseWheelSpeedMultiplier%
{
    MouseWheelSpeed--
    MouseWheelSpeedReal = %MouseWheelSpeed%
    MouseWheelSpeedReal *= %MouseWheelSpeedMultiplier%
}
If MouseWheelSpeedReal = 1
    ToolTip, Mouse wheel speed: %MouseWheelSpeedReal% line
else
    ToolTip, Mouse wheel speed: %MouseWheelSpeedReal% lines
SetTimer, RemoveToolTip, 1000
return

<ButtonWheelAccelerationSpeedUp>:
MouseWheelAccelerationSpeed++
RegRead, MouseWheelSpeedMultiplier, HKCU, Control Panel\Desktop, WheelScrollLines
If MouseWheelSpeedMultiplier <= 0
    MouseWheelSpeedMultiplier = 1
MouseWheelAccelerationSpeedReal = %MouseWheelAccelerationSpeed%
MouseWheelAccelerationSpeedReal *= %MouseWheelSpeedMultiplier%
ToolTip, Mouse wheel acceleration speed: %MouseWheelAccelerationSpeedReal% lines
SetTimer, RemoveToolTip, 1000
return
<ButtonWheelAccelerationSpeedDown>:
RegRead, MouseWheelSpeedMultiplier, HKCU, Control Panel\Desktop, WheelScrollLines
If MouseWheelSpeedMultiplier <= 0
    MouseWheelSpeedMultiplier = 1
If MouseWheelAccelerationSpeed > 1
{
    MouseWheelAccelerationSpeed--
    MouseWheelAccelerationSpeedReal = %MouseWheelAccelerationSpeed%
    MouseWheelAccelerationSpeedReal *= %MouseWheelSpeedMultiplier%
}
If MouseWheelAccelerationSpeedReal = 1
    ToolTip, Mouse wheel acceleration speed: %MouseWheelAccelerationSpeedReal% line
else
    ToolTip, Mouse wheel acceleration speed: %MouseWheelAccelerationSpeedReal% lines
SetTimer, RemoveToolTip, 1000
return

<ButtonWheelMaxSpeedUp>:
MouseWheelMaxSpeed++
RegRead, MouseWheelSpeedMultiplier, HKCU, Control Panel\Desktop, WheelScrollLines
If MouseWheelSpeedMultiplier <= 0
    MouseWheelSpeedMultiplier = 1
MouseWheelMaxSpeedReal = %MouseWheelMaxSpeed%
MouseWheelMaxSpeedReal *= %MouseWheelSpeedMultiplier%
ToolTip, Mouse wheel maximum speed: %MouseWheelMaxSpeedReal% lines
SetTimer, RemoveToolTip, 1000
return
<ButtonWheelMaxSpeedDown>:
RegRead, MouseWheelSpeedMultiplier, HKCU, Control Panel\Desktop, WheelScrollLines
If MouseWheelSpeedMultiplier <= 0
    MouseWheelSpeedMultiplier = 1
If MouseWheelMaxSpeed > 1
{
    MouseWheelMaxSpeed--
    MouseWheelMaxSpeedReal = %MouseWheelMaxSpeed%
    MouseWheelMaxSpeedReal *= %MouseWheelSpeedMultiplier%
}
If MouseWheelMaxSpeedReal = 1
    ToolTip, Mouse wheel maximum speed: %MouseWheelMaxSpeedReal% line
else
    ToolTip, Mouse wheel maximum speed: %MouseWheelMaxSpeedReal% lines
SetTimer, RemoveToolTip, 1000
return

<ButtonWheelUp>:
<ButtonWheelDown>:

If Button <> 0
{
    If Button <> %A_ThisHotkey%
    {
        MouseWheelCurrentAccelerationSpeed = 0
        MouseWheelCurrentSpeed = %MouseWheelSpeed%
    }
}
StringReplace, Button, A_ThisHotkey, *

ButtonWheelAccelerationStart:
If MouseWheelAccelerationSpeed >= 1
{
    If MouseWheelMaxSpeed > %MouseWheelCurrentSpeed%
    {
        Temp = 0.001
        Temp *= %MouseWheelAccelerationSpeed%
        MouseWheelCurrentAccelerationSpeed += %Temp%
        MouseWheelCurrentSpeed += %MouseWheelCurrentAccelerationSpeed%
    }
}

If Button = e
    MouseClick, wheelup,,, %MouseWheelCurrentSpeed%, 0, D
else if Button = f
    MouseClick, wheeldown,,, %MouseWheelCurrentSpeed%, 0, D

SetTimer, ButtonWheelAccelerationEnd, 100
return

ButtonWheelAccelerationEnd:
GetKeyState, kstate, %Button%, P
if kstate = D
    Goto ButtonWheelAccelerationStart

MouseWheelCurrentAccelerationSpeed = 0
MouseWheelCurrentSpeed = %MouseWheelSpeed%
Button = 0
return

RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
return
